//
//  ViewController.m
//  NSSet集合
//
//  Created by YYY on 2018/1/16.
//  Copyright © 2018年 YYY. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
  
    //1:不可变集合
    [self createImutableSet];
    
    //2:可变集合
    [self createMutableSet];
    
    //3:索引集合
    [self createIndexSet];

}

//不可变集合
- (void)createImutableSet
{
    NSSet *set = [NSSet setWithObjects:@"hello",@"world",@"year",nil];
    NSInteger count = [set count];
    NSLog(@"set = %@", set);
    NSLog(@"count = %ld", count);
    
    NSEnumerator *enumerator = [set objectEnumerator];
    for (NSObject *obj in enumerator)
    {
        NSLog(@"obj = %@", obj);
    }
    
    //是否存在
    BOOL isExist = [set containsObject:@"year"];
    NSLog(@"isExist = %d", isExist);
    
    //返回任意一个元素
    NSString *anyObject = [set anyObject];
    NSLog(@"anyObject = %@", anyObject);
    
    //判断是否包含这个元素
    NSObject *object = [set member:@"hello"];
    if (object)
    {
        NSLog(@"存在元素 = %@", object);
    }
    
    //简单创建
    NSSet *newSet = [[NSSet alloc] initWithArray:@[@10,@20.09,@99,@"happy"]];
    
    //判断两个集合是否相等
    BOOL isEqual = [set isEqualToSet:newSet];
    NSLog(@"是否相等 = %d", isEqual);
    
    //判断两个集合是否交集
    BOOL intersect = [set intersectsSet:newSet];
    NSLog(@"是否交集 = %d", intersect);
}
//可变集合
- (void)createMutableSet
{
    NSMutableSet *mutableSet = [[NSMutableSet alloc]initWithCapacity:5];
    [mutableSet addObject:@5];
    [mutableSet addObjectsFromArray:@[@10,@20,@"hate"]];
    NSLog(@"mutableSet1 = %@", mutableSet);
    
    //把NSSet增加进去
    [mutableSet unionSet:[NSSet setWithObjects:@"ios",@1000, nil]];
    NSLog(@"mutableSet2 = %@", mutableSet);
    
    //去除元素
    [mutableSet removeObject:@1000];
    
    //删除NSSet里面的
    [mutableSet minusSet:[NSSet setWithObjects:@"hate",@1000, nil]];
    
    //NSSet转化成NSMutableSet
    NSSet *set = [NSSet setWithObjects:@"hello",@"world",@"year",nil];
   
    NSMutableSet *mutableSet1 = [NSMutableSet set];
    [mutableSet1 setSet:set];
    NSLog(@"转换后 = %@", mutableSet1);
}

//索引集合
- (void)createIndexSet
{
    NSIndexSet *indexSet = [[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(2, 2)];
    NSArray *array = [[NSArray alloc] initWithObjects:@"good",@"nice",@"next",@"great", nil];
    NSArray *newArray = [array objectsAtIndexes:indexSet];
    NSLog(@"newArray = %@", newArray);
    
    NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init];
    [mutableIndexSet addIndex:0];
    [mutableIndexSet addIndex:3];
    [mutableIndexSet addIndex:5];
    
    //通过集合获取数组中指定的元素
    NSArray *tempArray1 = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four",@"five",@"six", nil];
    NSArray *tempArray2 = [tempArray1 objectsAtIndexes:mutableIndexSet];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
